.TH std::chrono::time_point 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::chrono::time_point \- std::chrono::time_point

.SH Synopsis
   Defined in header <chrono>
   template<

       class Clock,                               \fI(since C++11)\fP
       class Duration = typename Clock::duration

   > class time_point;

   Class template std::chrono::time_point represents a point in time. It is implemented
   as if it stores a value of type Duration indicating the time interval from the start
   of the Clock's epoch.

   Clock must meet the requirements for Clock
   or be std::chrono::local_t                 (until C++23)
   \fI(since C++20)\fP.

.SH Member types

   Member type Definition
   clock       Clock, the clock on which this time point is measured
   duration    Duration, a std::chrono::duration type used to measure the time since
               epoch
   rep         Rep, an arithmetic type representing the number of ticks of the duration
   period      Period, a std::ratio type representing the tick period of the duration

.SH Member functions

   constructor      constructs a new time point
                    \fI(public member function)\fP
   time_since_epoch returns the time point as duration since the start of its clock
                    \fI(public member function)\fP
   operator+=       modifies the time point by the given duration
   operator-=       \fI(public member function)\fP
   operator++
   operator++(int)  increments or decrements the duration
   operator--       \fI(public member function)\fP
   operator--(int)
   (C++20)
   min              returns the time point corresponding to the smallest duration
   \fB[static]\fP         \fI(public static member function)\fP
   max              returns the time point corresponding to the largest duration
   \fB[static]\fP         \fI(public static member function)\fP

.SH Non-member functions

   operator+                      performs add and subtract operations involving a time
   operator-                      point
   \fI(C++11)\fP                        \fI(function template)\fP
   operator==
   operator!=
   operator<
   operator<=
   operator>
   operator>=
   operator<=>                    compares two time points
   \fI(C++11)\fP                        \fI(function template)\fP
   \fI(C++11)\fP(removed in C++20)
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   (C++20)
   time_point_cast                converts a time point to another time point on the
   \fI(C++11)\fP                        same clock, with a different duration
                                  \fI(function template)\fP
   floor(std::chrono::time_point) converts a time_point to another, rounding down
   \fI(C++17)\fP                        \fI(function template)\fP
   ceil(std::chrono::time_point)  converts a time_point to another, rounding up
   \fI(C++17)\fP                        \fI(function template)\fP
   round(std::chrono::time_point) converts a time_point to another, rounding to
   \fI(C++17)\fP                        nearest, ties to even
                                  \fI(function template)\fP

.SH Helper classes

   std::common_type<std::chrono::time_point> specializes the std::common_type trait
   \fI(C++11)\fP                                   \fI(class template specialization)\fP
   std::hash<std::chrono::time_point>        hash support for std::chrono::time_point
   (C++26)                                   \fI(class template specialization)\fP

.SH Example


// Run this code

 #include <algorithm>
 #include <chrono>
 #include <ctime>
 #include <iomanip>
 #include <iostream>

 void slow_motion()
 {
     static int a[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
     // Generate Γ(13) == 12! permutations:
     while (std::ranges::next_permutation(a).found) {}
 }

 int main()
 {
     using namespace std::literals; // enables literal suffixes, e.g. 24h, 1ms, 1s.

     const std::chrono::time_point<std::chrono::system_clock> now =
         std::chrono::system_clock::now();

     const std::time_t t_c = std::chrono::system_clock::to_time_t(now - 24h);
     std::cout << "24 hours ago, the time was "
               << std::put_time(std::localtime(&t_c), "%F %T.\\n") << std::flush;

     const std::chrono::time_point<std::chrono::steady_clock> start =
         std::chrono::steady_clock::now();

     std::cout << "Different clocks are not comparable: \\n"
                  "  System time: " << now.time_since_epoch() << "\\n"
                  "  Steady time: " << start.time_since_epoch() << '\\n';

     slow_motion();

     const auto end = std::chrono::steady_clock::now();
     std::cout
         << "Slow calculations took "
         << std::chrono::duration_cast<std::chrono::microseconds>(end - start) << " ≈ "
         << (end - start) / 1ms << "ms ≈ " // almost equivalent form of the above, but
         << (end - start) / 1s << "s.\\n";  // using milliseconds and seconds accordingly
 }

.SH Possible output:

 24 hours ago, the time was 2021-02-15 18:28:52.
 Different clocks are not comparable:
   System time: 1666497022681282572ns
   Steady time: 413668317434475ns
 Slow calculations took 2090448µs ≈ 2090ms ≈ 2s.

.SH See also

   duration       a time interval
   \fI(C++11)\fP        \fI(class template)\fP
   year_month_day represents a specific year, month, and day
   (C++20)        \fI(class)\fP
